package PriorityQueue;

import java.util.*;

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
    public int compareTo(Student o) {
        //return this.age - o.age;  //小根堆
        return o.age - this.age;    //大根堆
    }
}

//姓名比较器
class NameComparetor implements Comparator<Student>{
    @Override
    public int compare(Student o1,Student o2) {
        //return o1.name.compareTo(o2.name); //小根堆
        return o2.name.compareTo(o1.name);  //大根堆
    }
}

//年龄比较器
class AgeComparetor implements Comparator<Student>{
    @Override
    public int compare(Student o1,Student o2) {
        return o1.age - o2.age;
    }
}

class IntCmp implements Comparator<Integer>{
    @Override
    public int compare(Integer o1, Integer o2) {
        //return o1.compareTo(o2); //小根堆
        return o2.compareTo(o1); //大根堆
    }
}

public class Test {
    public int[] smallestK(int[] arr, int k) {
        int[]  ret = new int[k];
        if(arr == null || k == 0){
            return ret;
        }
        Queue<Integer> minHeap =new PriorityQueue<>(k);
        for (int x:arr) {
            minHeap.offer(x);
        }
        for (int i = 0; i < k; i++) {
            ret[i] = minHeap.poll();
        }
        return ret;
    }

    /**
     * @param arr
     * @param k
     * @description 前k个最大的元素 N*logK
     */
    public static int[] maxK(int[] arr,int k){
        int[]  ret = new int[k];
        if(arr == null || k == 0){
            return ret;
        }
        Queue<Integer> minHeap =new PriorityQueue<>(k);
        //1. 遍历数组的前k个 放到堆当中 O(k*logK)
        for (int i = 0; i < k; i++) {
            minHeap.offer(arr[i]);
        }
        //2. 遍历剩下的k-1个，每次和堆顶元素进行比较
         //堆顶元素小：出堆    (N-k)*logK
        for (int i = k; i < arr.length; i++) {
            if(arr[i] >minHeap.peek()){
                minHeap.poll();
                minHeap.offer(arr[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = minHeap.poll();
        }
        return ret;
    }


    public static void main(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        testHeap.initElem(array);
        testHeap.createHeap(); //变成大根堆了
        testHeap.heapSort();
        System.out.println("///");


    }

    public static void main6(String[] args) {
        NameComparetor nameComparetor = new NameComparetor();
        Queue<Student> priorityQueue =new PriorityQueue<>(nameComparetor);
        priorityQueue.offer(new Student("zhangsan",15));
        priorityQueue.offer(new Student("lisi",55));
        System.out.println("///");

    }
    public static void main7(String[] args) {
        //1.第一种创建构造器
        IntCmp intCmp = new IntCmp();
        Queue<Integer> priorityQueue =new PriorityQueue<>(intCmp);


        //2.第二种创建构造器
        //匿名内部类
        Queue<Integer> priorityQueue2 =new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });

        //3.第三种创建构造器
        //lambda表达式
        Queue<Integer> priorityQueue3 =new PriorityQueue<>(((o1, o2) -> {return o2.compareTo(o1);}));


        priorityQueue.offer(1);
        priorityQueue.offer(99);
        System.out.println("*****");
    }

    public static void main5(String[] args) {
        Student student1 = new Student("zhangsan",15);
        Student student2 = new Student("zhangsan",15);
        //System.out.println(student1.compareTo(student2));

        NameComparetor nameComparetor = new NameComparetor();
        System.out.println(nameComparetor.compare(student1, student2));


    }


    public static void main4(String[] args) {
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        int[] ret = maxK(array,3);
        System.out.println(Arrays.toString(ret)); //[37, 49, 65]

    }
    public static void main3(String[] args) {
        List<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        Queue<Integer> priorityQueue =new PriorityQueue<>(list);
        priorityQueue.offer(10);
        priorityQueue.offer(2);
        System.out.println("*****");
    }
    public static void main2(String[] args) {
        Queue<Integer> priorityQueue =new PriorityQueue<>();
        priorityQueue.offer(10);
        priorityQueue.offer(2);
      //  System.out.println(priorityQueue.poll()); //2 说明，底层代码是小根堆

        Queue<Student> priorityQueue2 =new PriorityQueue<>();
        priorityQueue2.offer(null);
     /*  priorityQueue2.offer(new Student("zhangsan",15));
        priorityQueue2.offer(new Student("lisi",55));*/
    }


    public static void main1(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        testHeap.initElem(array);
        testHeap.createHeap();
        //testHeap.offer(80);//插入元素
        //testHeap.poll();
        System.out.println(testHeap.peek());


    }
}
